﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M006GoodsGroupEdit : FormBase
    {
        #region 变量定义

        private readonly XGWMS.BLL.BLL_M006_GOODS_GROUP bll_M006_GOODS_GROUP;
        private readonly MODEL.MOD_M006_GOODS_GROUP entityGoodsGroup;

        /// <summary>
        /// 商品分类
        /// </summary>
        private const string dataName = "商品分类";

        #endregion

        #region 方法

        /// <summary>
        /// 构造函数
        /// </summary>
        public M006GoodsGroupEdit()
        {
            InitializeComponent();
            this.txtGOODS_GROUP_ID.Text = "";
            this.txtGOODS_GROUP_NM.Text = "";
            this.txtMEMO.Text = "";
            this.txtOrderNum.Text = "";
            this.txtGOODS_GROUP_ID.Enabled = false;
            entityGoodsGroup = new XiaoGang.Soft.XGWMS.MODEL.MOD_M006_GOODS_GROUP();
            bll_M006_GOODS_GROUP = new XiaoGang.Soft.XGWMS.BLL.BLL_M006_GOODS_GROUP();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="GroupID"></param>
        /// <param name="GroupName"></param>
        /// <param name="GroupMemo"></param>
        /// <param name="OrderNum"></param>
        public M006GoodsGroupEdit(string GroupID, string GroupName, string GroupMemo, string OrderNum)
        {
            InitializeComponent();
            this.txtGOODS_GROUP_ID.Text = GroupID;
            this.txtGOODS_GROUP_NM.Text = GroupName;
            this.txtMEMO.Text = GroupMemo;
            this.txtOrderNum.Text = OrderNum;
            this.txtGOODS_GROUP_ID.Enabled = false;
            entityGoodsGroup = new XiaoGang.Soft.XGWMS.MODEL.MOD_M006_GOODS_GROUP();
            bll_M006_GOODS_GROUP = new XiaoGang.Soft.XGWMS.BLL.BLL_M006_GOODS_GROUP();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="isNew"></param>
        public M006GoodsGroupEdit(bool isNew)
        {
            InitializeComponent();
            entityGoodsGroup = new XiaoGang.Soft.XGWMS.MODEL.MOD_M006_GOODS_GROUP();
            bll_M006_GOODS_GROUP = new XiaoGang.Soft.XGWMS.BLL.BLL_M006_GOODS_GROUP();
        }

        /// <summary>
        /// <p><b>方法名称</b>ClearControl</p>	
        /// <p><b>概要説明	:</b>画面上の全項目をオールクリアする</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/26</p>	
        /// </summary>	
        protected void ClearControl()
        {
            txtGOODS_GROUP_NM.Text = "";
            txtMEMO.Text = "";
            txtOrderNum.Text = "";
        }

        /// <summary>	
        /// <p><b>方法名称</b>SetTabIndex</p>	
        /// <p><b>概要説明	:</b>画面フォーカスの遷移順の設定</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/26</p>	
        /// </summary>	
        protected void SetTabIndex()
        {
            int intIndex = 0;
            txtGOODS_GROUP_NM.TabIndex = intIndex; intIndex++;
            txtOrderNum.TabIndex = intIndex; intIndex++;
            txtMEMO.TabIndex = intIndex; intIndex++;
            btnClear.TabIndex = intIndex; intIndex++;
            btnInsert.TabIndex = intIndex; intIndex++;            
            btnExit.TabIndex = intIndex; intIndex++;
        }

        /// <summary>	
        /// <p><b>方法名称</b>SetEnable</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/26</p>	
        /// <p><b>修改历史:</b></p>	
        /// </summary>        
        protected void SetEnable()
        {
            grpGoodsGroupInfo.Enabled = true;
            txtGOODS_GROUP_NM.Enabled = true;
            txtOrderNum.Enabled = true;
            txtMEMO.Enabled = true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>InitProcess</p>	
        /// <p><b>概要説明	:</b>窗口初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/26</p>	
        /// </summary>
        protected void InitProcess()
        {
            SetTabIndex();
            SetEnable();
        }
        
        /// <summary>
        /// <p><b>方法名称</b>CheckProcess</p>
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>
        /// <p><b>作成者	:</b>万岗</p>
        /// <p><b>作成日	:</b>2008/08/26</p>
        /// <p><b>修改历史:</b></p>
        /// </summary>
        /// <param type="string" name="strControlName"></param>
        /// <returns type = "Boolean">　True:成功   False:失敗 </returns>
        protected override bool CheckProcess(string strControlName)
        {
            switch (strControlName)
            {
                case "txtGOODS_GROUP_ID":
                    if (Method.CheckIsNumber(txtGOODS_GROUP_ID.Text.Trim(),txtGOODS_GROUP_ID.MaxLength, false, true) == false)
                    {
                        Method.MessageShow(GlobalVal.glostrMsg, dataName);
                        txtGOODS_GROUP_ID.Focus();
                        return false;
                    }
                    break;
                case "txtGOODS_GROUP_NM":
                    if (0 == txtGOODS_GROUP_NM.Text.Trim().Length)
                    {
                        Method.MessageShow("W11E02", "分类名称");//[@]不能为空！
                        txtGOODS_GROUP_NM.Focus();
                        return false;
                    }
                    if (Encoding.Default.GetByteCount(txtGOODS_GROUP_NM.Text.Trim()) >
                        txtGOODS_GROUP_NM.MaxLength)
                    {
                        Method.MessageShow("W11007", dataName);
                        txtGOODS_GROUP_NM.Focus();
                        return false;
                    }
                    if (Method.CheckIsIncludeSpecailChar(Method.ToDBC(
                        txtGOODS_GROUP_NM.Text.Trim())) == false)
                    {
                        Method.MessageShow(GlobalVal.glostrMsg, dataName);
                        txtGOODS_GROUP_NM.Focus();
                        return false;                   
                    }
                    break;
                case "txtOrderNum":
                    string strValue = this.txtOrderNum.Text.Trim().Replace(",", "");
                    if (!Method.CheckIsNumber(strValue, txtOrderNum.MaxLength, true, true))
                    {
                        Method.MessageShow(GlobalVal.glostrMsg, dataName);
                        txtOrderNum.Focus();
                        return false;
                    }
                    break;
                case "txtMEMO":
                    if (Encoding.Default.GetByteCount(txtMEMO.Text.Trim()) > txtMEMO.MaxLength)
                    {
                        Method.MessageShow("W11007", dataName);
                        txtMEMO.Focus();
                        return false;
                    }
                    if (Method.CheckIsIncludeSpecailChar(Method.ToDBC(txtMEMO.Text.Trim())) == false)
                    {
                        Method.MessageShow(GlobalVal.glostrMsg, dataName);
                        txtMEMO.Focus();
                        return false;
                    }
                    break; 
            }
            return true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>CheckProcessLoop</p>	
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/08/26</p>
        /// <p><b>修改历史:</b></p>	
        /// </summary>
        /// <param type="string" name="strBtnName"></param>
        /// <returns type = "Boolean">　True:成功   False:失敗 </returns>
        protected bool CheckProcessLoop(enumOperation emOperation)
        {
            switch (emOperation)
            {               
                case enumOperation.Add:
                    if (CheckProcess("txtGOODS_GROUP_NM") == false) return false;
                    if (CheckProcess("txtOrderNum") == false) return false;
                    if (CheckProcess("txtMEMO") == false) return false;
                    if (CheckNameAdd() == false) return false;
                    break;
                case enumOperation.Update:
                    if (CheckProcess("txtGOODS_GROUP_ID") == false) return false;
                    if (CheckProcess("txtGOODS_GROUP_NM") == false) return false;
                    if (CheckProcess("txtOrderNum") == false) return false;
                    if (CheckProcess("txtMEMO") == false) return false;
                    if (CheckNameUpdate() == false) return false;
                    break;
            }
            return true;
        }        

        /// <summary>	
        /// <p><b>方法名称</b>GoodsGroupDeleteCheck</p>	
        /// <p><b>概要説明  :</b>检测该分类有没有没使用</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/09/17</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">商品分类编号</param>
        /// <returns type = "bool">false:使用，true:没有使用 </returns>
        protected bool GoodsGroupDeleteCheck(string strGoodsGroupID)
        {
            ReturnValue rvReturnValue = bll_M006_GOODS_GROUP.GoodsGroupDeleteCheck(strGoodsGroupID);
            if (!rvReturnValue.IsSuccess)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);                
                return false;
            }
            else                
            {
                if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    Method.MessageShow ("W11028",dataName);//本分类正在使用,不能删除!
                    txtGOODS_GROUP_ID.Focus ();
                    txtGOODS_GROUP_ID.SelectAll ();
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        /// <summary>	
        /// <p><b>方法名称</b>CheckNameAdd</p>	
        /// <p><b>概要説明  :</b>添加时对商品分类名称存在的Check</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/09/17</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <returns type = "bool">false:存在，true:不存在</returns>
        protected bool CheckNameAdd()
        {
            ReturnValue rvReturnValue = bll_M006_GOODS_GROUP.CheckName(txtGOODS_GROUP_NM.Text.Trim());
            if (!rvReturnValue.IsSuccess)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);
                return false;
            }
            if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count>0)
            {
                Method.MessageShow("W11H01", dataName);//本[@]已经存在或已被逻辑删除!
                txtGOODS_GROUP_NM.Focus();
                return false;
            }
            return true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>CheckNameAdd</p>	
        /// <p><b>概要説明  :</b>更新时对商品分类名称存在的Check</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/09/17</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <returns type = "bool">false:存在，true:不存在</returns>
        protected bool CheckNameUpdate()
        {
            ReturnValue rvReturnValue = bll_M006_GOODS_GROUP.CheckGoodsGroupNameWithoutID(txtGOODS_GROUP_ID.Text.Trim(),txtGOODS_GROUP_NM.Text.Trim());
            if (rvReturnValue.IsSuccess == false)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);
                return false;
            }
            if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count>0)
            {
                Method.MessageShow("W11H01", dataName);//本[@]已经存在或已被逻辑删除!
                txtGOODS_GROUP_NM.Focus();
                return false;
            }
            return true;
        }

        #endregion

        #region 事件

        private void GOODS_GROUP_006_Load(object sender, EventArgs e)
        {
            InitProcess();
            txtGOODS_GROUP_NM.Focus();
        }

        private void AllButton_Click(object sender, EventArgs e)
        {
            if (Cursor.Current == Cursors.WaitCursor)
            {
                return;
            }
            string strButtonName = ((Button)sender).Name;
            switch (strButtonName)
            {
                #region btnInsert

                case "btnInsert":
                    {
                        if (txtGOODS_GROUP_ID.Text.Trim().Length < 1 && !this.CheckProcessLoop(enumOperation.Add))
                        {
                            return;
                        }
                        if (txtGOODS_GROUP_ID.Text.Trim().Length > 0 && !this.CheckProcessLoop(enumOperation.Update))
                        {
                            return;
                        }
                        Cursor.Current = Cursors.WaitCursor;
                        entityGoodsGroup.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID.Trim();
                        entityGoodsGroup.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD.Trim();
                        entityGoodsGroup.GOODS_GROUP_ID = txtGOODS_GROUP_ID.Text.Trim();
                        entityGoodsGroup.GOODS_GROUP_NM = txtGOODS_GROUP_NM.Text.Trim();
                        entityGoodsGroup.MEMO = txtMEMO.Text.Trim();
                        if (txtOrderNum.Text.Trim().Length < 1)
                        {
                            entityGoodsGroup.OrderNum = null;
                        }
                        else
                        {
                            entityGoodsGroup.OrderNum = Int32.Parse(txtOrderNum.Text.Trim());
                        }
                        ReturnValue result = bll_M006_GOODS_GROUP.AddOrUpdateProcess(entityGoodsGroup);
                        Cursor.Current = Cursors.Default;
                        if (result.IsSuccess)
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            Method.MessageShow(result.ErrorID, dataName);
                            txtGOODS_GROUP_NM.Focus();
                        }
                        break;
                    }

                #endregion
                
                #region btnClear

                case "btnClear":
                    {
                        ClearControl();                        
                        txtGOODS_GROUP_NM.Focus();
                    }
                    break;

                #endregion

                #region btnExit

                case "btnExit":
                    {
                        //if (Method.MessageShow("Q12006") == DialogResult.Yes)
                        //{
                            this.Close();
                        //}
                    }
                    break;

                #endregion
            }
            Cursor.Current = Cursors.Default;
        }

        private void AllTextBox_Focus(object sender, EventArgs e)
        {            
            try
            {
                switch (((TextBox)sender).Name)
                {
                    case "txtGOODS_GROUP_ID":
                        ((TextBox)sender).SelectAll();
                        ((TextBox)sender).ImeMode = ImeMode.Disable;
                        break;
                    case "txtGOODS_GROUP_NM":
                        ((TextBox)sender).SelectAll();
                        ((TextBox)sender).ImeMode = ImeMode.Hiragana;
                        break;
                }
            }
            catch
            {                
                txtMEMO.ImeMode = ImeMode.Hiragana;
            }
        }

        private void M006_GOODS_GROUP_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F5:
                    AllButton_Click(btnInsert, e);
                    break;
                case Keys.F12:
                    AllButton_Click(btnClear, e);
                    break;
                case Keys.Escape:
                    AllButton_Click(btnExit, e);
                    break; 
            }
        }

        #endregion
    }
}